package com.example.villagemarket.adapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.example.villagemarket.R;
import com.example.villagemarket.R.layout;


import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;

public class ResultListView extends Activity {

	static final String ITEM_PRICE = "price";
	static final String ITEM_ID = "id";
	static final String ITEM_DESCRIPTION = "description";
	static final String ITEM_QUANTITY = "quantity";

	ResultsAdapter adapter;

	ArrayList<HashMap<String, String>> itemsList;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_search_fragment);

	}

	public ArrayList<HashMap<String, String>> populateListView(
			List<Map<String, String>> results, ListView list, Integer imageId) {
		itemsList = new ArrayList<HashMap<String, String>>();

		for (int i = 0; i < results.size(); i++) {

			HashMap<String, String> map = new HashMap<String, String>();

			String temp = results.get(i).get("item");
			String[] tempArray;
			tempArray = temp.split("~");

			// The order returned from search.php is
			// description~quantity~price~priceA

			String priceToUse;
			
			//Checks if quantity equals 0
			if(!tempArray[1].equals("0"))
			{
				//Checks if priceA equals 0
				if(!tempArray[3].equals("0"))
				{
					priceToUse = tempArray[3];
				}
				else
				{	
					priceToUse = tempArray[2];
				}
				
				if(Double.parseDouble(tempArray[1]) > 0)
				{
					map.put(ITEM_ID, i + "");
					map.put(ITEM_DESCRIPTION, tempArray[0]);
					map.put(ITEM_QUANTITY,tempArray[1]);
					map.put(ITEM_PRICE,priceToUse);
					

					itemsList.add(map);
				}
				
			}
			

			

		}

		return itemsList;

	}


}